package com.syni.mdd.yhd.common.all.quartz;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * @className BmsUserVideoLikeQuartz
 * @description 定时任务（一天一次，扫描的商家点赞数）
 * @Author cfx
 * @DATE 2019/4/18 16:57
 * @VERSION 1.0
 **/
@Component
@EnableScheduling
public class BmsUseDkLikeQuartz {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BmsVideoService bmsVideoService;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Scheduled(cron = "0 0 0 * * ?")  //每天晚上凌晨执行一次定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    public void countVideoLikeTimes(){
        String redisKey = Constants.REDIS_KEY_LIKEDK;
        long size = redisTemplate.opsForZSet().size(redisKey);
        if (size > 0) {
            Set<ZSetOperations.TypedTuple> setRedis = redisTemplate.opsForZSet().rangeWithScores(redisKey, 0, size);
            for (ZSetOperations.TypedTuple typedTuple : setRedis) {
                String[] dkLikeKey = typedTuple.getValue().toString().split(":");
                String identify = dkLikeKey[0];
                Integer id = Integer.parseInt(dkLikeKey[1]);
                Integer num = typedTuple.getScore().intValue();
                if (BmsVideoService.videoLike.equals(identify)) {
                    Optional<BmsVideo> bmsVideoOptional = bmsVideoService.getBaseDao().findById(id);
                    if(bmsVideoOptional.isPresent()) {
                        BmsVideo bmsVideo = bmsVideoOptional.get();
                        bmsVideo.setLikeTimes(bmsVideo.getLikeTimes() == null ? num : bmsVideo.getLikeTimes() + num);
                        if (bmsVideo.getLikeTimes().intValue() < 0) {
                            bmsVideo.setLikeTimes(0);
                        }
                        bmsVideoService.getBaseDao().save(bmsVideo);
                    }
                } else if(BmsVideoService.businessDk.equals(identify)){
                    BmsBusiness business = bmsBusinessService.getBaseDao().findById(id).get();
                    if (business != null) {
                        business.setDkTimes(business.getDkTimes() == null ? num : business.getDkTimes() + num);
                        if(business.getDkTimes().intValue() < 0){
                            business.setDkTimes(0);
                        }
                        bmsBusinessService.save(business);
                    }
                } else if(BmsVideoService.videoComment.equals(identify)){
                    Optional<BmsVideo> bmsVideoOptional = bmsVideoService.getBaseDao().findById(id);
                    if(bmsVideoOptional.isPresent()) {
                        BmsVideo bmsVideo = bmsVideoOptional.get();
                        bmsVideo.setCommentNum(bmsVideo.getCommentNum() == null ? num : bmsVideo.getCommentNum() + num);
                        if (bmsVideo.getCommentNum().intValue() < 0) {
                            bmsVideo.setCommentNum(0);
                        }
                        bmsVideoService.getBaseDao().save(bmsVideo);
                    }
                }
            }
            redisTemplate.delete(redisKey);
        }
    }
}
